#ifndef SERIALPORTTHREAD_H
#define SERIALPORTTHREAD_H

//#include <QThread>
//#include <QtSerialPort>
#include <QtSerialPort/QSerialPort>
#include <QDialog>
#include <QGridLayout>
#include <QPushButton>
#include <QComboBox>
#include <QSerialPortInfo>

class SerialPortThread : public QDialog
{
    Q_OBJECT
public:
    explicit SerialPortThread(QDialog *parent = nullptr);
    void serialWriteData(QByteArray &byteArray);

private:
    typedef struct
    {
        QString portName;
        QString systemLocation;
        QString description;
        QString manufacturer;
        QString serialNumber;
        QString hasVendorIdentifier;
        QString hasProductIdentifier;
    }_SerialPortInfo;
    // void run() override;
    QSerialPort serial;
    QString serialPortName;
    int serialPortBaud;
    bool quit;
    QGridLayout *pGridLayout;
    QComboBox *pComBoxPort;
    QComboBox *pComBoxBaud;
    std::vector<_SerialPortInfo> searialPortInfos;
    QPushButton *pBtn_connect;
    bool serialIsOpen = false;
    QByteArray searial_read_data;

    void disconnect(void);
    bool open_port(const QString portName,const int baud);
private:
    void update_serial_port(void);
signals:
    void error(const QString &s);
    void receiveData(QByteArray &byteArray);
    void sig_communication_connection(bool status);
    void sig_communication_disconnect(void);

public slots:
    void serialReadyRead(void);
    void on_btn_connect(void);
    void on_send_data(QByteArray data);
};

#endif // SERIALPORTTHREAD_H
